package 二叉树;

public class q101对称二叉树 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if(root==null) return true;
            return compare(root.left,root.right);
        }

        public boolean compare(TreeNode left , TreeNode right){
            if(left==null && right!=null) return false;
            else if(left!=null && right==null) return false;
            else if(left==null && right==null) return true;
            else if(left.val!=right.val) return  false;


            boolean outside = compare(left.left,right.right);
            boolean inside = compare(left.right,right.left);
            boolean same = outside && inside;
            return  same;
        }
    }
}
